
package net.lwf.string.util;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.StringTokenizer;

import net.lwf.common.constant.ICommonConstant;


public class StringManipulator {
	
	/**
	 * Generates comma separated string from the given list
	 * 
	 * @param lstData
	 * @return
	 */
	public static String generateString(List lstData) {
		String generated = "";
		for(int i=0; i<lstData.size(); i++) {
			Object obj = (Object)lstData.get(i);
			if(obj instanceof String) {
				generated += (String) lstData.get(i);	
			}
			else {
				generated += String.valueOf(obj);
			}
			
			if(i<lstData.size()-1) {
				generated += ", ";
			}
		}
		return generated;
	}	
	
	public static String generateCsStringFromIntArray(int[] values) {
		String generated = "";
		for(int i=0; i<values.length; i++) {
				generated += String.valueOf(values[i]);
			if(i<values.length-1) {
				generated += ", ";
			}
		}
		return generated;
	}		
	
	public static String removeCharacter(String value, String character) {
		String returnvalue = "";
		if(value != null && value.trim().length() != 0) {
			StringTokenizer stk = new StringTokenizer(value, character);
			while(stk.hasMoreTokens()) {
				returnvalue += stk.nextToken();
			}
			return returnvalue;
		}
		return value;
	} //removeCharacter
	
    /**
	 * Replace specified string literals with other string literals in the given 
	 * source string.
	 * 
	 * @param source       Source string to be modified
	 * @param find         String value to be replaced in the specified source string
	 * @param replacement  New value to be replaced
	 * 
	 * @return
	 */
	public static final String replace(String source, String find, String replacement) {
		int i = 0;
		int j;
		int k = find.length();
		int m = replacement.length();
		String strSource = source;

		while (i < strSource.length()) {
			j = strSource.indexOf(find, i);

			if (j == -1) {
				break;
			}

			strSource = replace(strSource, j, j + k, replacement);

			i = j + m;
		}

		return strSource;
	} //replace
	
	/**
	 * Replace specified string literals with other string literals in the given 
	 * source string at a specified location.
	 * 
	 * @param source      Source string to be modified
	 * @param start       Starting position
	 * @param end         Ending position
	 * @param replacement New value to be replaced
	 * @return
	 */
	private static final String replace(String source, int start, int end, String replacement) {
		
		String strSource = source;
		
		if (start == 0) {
			strSource = replacement + strSource.substring(end);
		} else if (end == source.length()) {
			strSource = strSource.substring(0, start) + replacement;
		} else {
			strSource =
			strSource.substring(0, start)
					+ replacement
					+ strSource.substring(end);
		}

		return strSource;
	} //replace
	
	public static String getOnlyNumerics(String str) {
		if (str == null) {
			return null;
		}
		StringBuffer strBuff = new StringBuffer();
		char c;
		for (int i = 0; i < str.length(); i++) {
			c = str.charAt(i);
			if (Character.isDigit(c)) {
				strBuff.append(c);
			}
		}
		return strBuff.toString();
	}
	
	public static String getOnlyCharacters(String str) {
		if (str == null) {
			return null;
		}
		StringBuffer strBuff = new StringBuffer();
		char c;
		for (int i = 0; i < str.length(); i++) {
			c = str.charAt(i);
			if (!Character.isDigit(c)) {
				strBuff.append(c);
			}
		}
		return strBuff.toString();
	}
	
	public static String replaceAWord(String source, String find, String replace) {
		String newword = "";
		if(source != null && source.trim().length() != 0) {
			StringTokenizer stk = new StringTokenizer(source, " ");
			while(stk.hasMoreTokens()) {
				String word = stk.nextToken();
				if(word.equalsIgnoreCase(find)) {
					newword += replace + " ";
				}
				else {
					newword += word + " ";
				}
			}
		}
		return newword.trim();
	}
	
	public static int countOccurrences(String arg1, String arg2) {
        int count = 0;
        int index = 0;
        while ((index = arg1.indexOf(arg2, index)) != -1) {
             ++index;
             ++count;
        }
        return count;
   }
	
	/**
	 * Reverses comma separated string
	 * 
	 * @param value
	 * @return
	 */
	public static String reverse(String value) {
		String ret_str = "";

		int counter = 0;
		
		List<String> items = Arrays.asList(value.split(","));
		//List<String> items = Arrays.asList(value.split("\\s*,\\s*"))

		Collections.reverse(items);
		for(String item: items) {
			ret_str += item.trim();
			if(counter+1 < items.size()) {
				ret_str += ICommonConstant.COMMA + ICommonConstant.EMPTY;
			}
			counter++;			
		}
		
		return ret_str;
	}
	
	/**
	 * Generates comma separated string from the list of integer values.
	 * 
	 * @param choices
	 * @return
	 */
	public static String generateCsvString(List<Integer> choices) {
		String answer_ret = ICommonConstant.EMPTY;
		int counter = 0;
		for(Integer choice: choices) {
			answer_ret += choice.toString();
			if(counter + 1 < choices.size()) {
				answer_ret += ICommonConstant.COMMA + ICommonConstant.EMPTY;;
			}
			counter++;
		}
		return answer_ret;
	}
	
	public static String shuffleCsString(String csvString) {
		String ret_str = "";
		List<String> items = Arrays.asList(csvString.split(","));
		Collections.shuffle(items);
		int counter = 0;
		for(String item: items) {
			ret_str += item.trim();
			if(counter+1 < items.size()) {
				ret_str += ", ";
			}
			counter++;			
		}
		return ret_str;
	}
	
	public static void main(String[] args) {
		String input = "Fives and ones";
		System.out.println(replace(input, " ", ""));
	}

} //StringManipulator
